const BASE_URL = '';
const json2url = (json) => {
    let arr = [];
    for (let name in json) {
        arr.push(name + '=' + json[name]);
    }
    return arr.join('&')
}
const http = ({ url, method = 'get', params = {}, data = {} }) => {
    if (!url) return;

    const token = sessionStorage.getItem('token') || '';
    let option = {
        method,
        headers: {
            token,
            'Content-Type': 'application/json'
        }
    }
    if (method == 'post') {
        option.body = JSON.stringify(data);
    }
    return fetch(BASE_URL + url + '?' + json2url(params), option).then((res) => {
        console.log(res);
        if (res.status == 401) {
            window.location.href = '/login';
        } else {
            return res.json();
        }

    })
}

export default http;


